<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言基础——预处理相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-14-C语言-预处理">
<meta property="og:url" content="https://sumumm.github.io/post/73483bf2.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言基础——预处理相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-03T07:08:36.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.985Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://sumumm.github.io/post/73483bf2.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/73483bf2.html","path":"post/73483bf2.html","title":"LV01-14-C语言-预处理"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-14-C语言-预处理 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E9%A2%84%E5%A4%84%E7%90%86%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">一、预处理的概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%A2%84%E5%AE%9A%E4%B9%89"><span class="nav-text">二、预定义</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%8F"><span class="nav-text">1.宏</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%A2%84%E5%AE%9A%E4%B9%89%E7%AC%A6%E5%8F%B7%E4%B8%B2"><span class="nav-text">2.预定义符号串</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">3.宏定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E6%97%A0%E5%8F%82%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">3.1无参宏定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E5%90%AB%E5%8F%82%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">3.2含参宏定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E5%AE%8F%E4%B8%8E%E5%87%BD%E6%95%B0"><span class="nav-text">3.3宏与函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E4%B8%8E"><span class="nav-text">3.4 # 与 ## </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-%E7%9A%84%E7%94%A8%E9%80%94"><span class="nav-text">3.4.1 # 的用途</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-2-%E7%9A%84%E7%94%A8%E9%80%94"><span class="nav-text">3.4.2 ## 的用途</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB"><span class="nav-text">三、文件包含</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%9D%A1%E4%BB%B6%E7%BC%96%E8%AF%91"><span class="nav-text">四、条件编译</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-if"><span class="nav-text">1. #if </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-ifdef"><span class="nav-text">2. #ifdef </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ifndef"><span class="nav-text">3. #ifndef </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81-pragma"><span class="nav-text">五、 #pragma </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1%E7%AE%80%E4%BB%8B"><span class="nav-text">4.1简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-message"><span class="nav-text">4.2 message </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-1%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="nav-text">4.2.1语法格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-once"><span class="nav-text">4.3 once </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-1%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="nav-text">4.3.1语法格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-4-pack"><span class="nav-text">4.4 pack </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-1%E5%86%85%E5%AD%98%E5%AF%B9%E9%BD%90"><span class="nav-text">4.4.1内存对齐</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-2%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="nav-text">4.4.2语法格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.4.2使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81-error"><span class="nav-text">六、 #error </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.使用格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.使用实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E6%98%93%E9%94%99%E7%82%B9"><span class="nav-text">七、易错点</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A1%88%E4%BE%8B1"><span class="nav-text">1.案例1</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/73483bf2.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-14-C语言-预处理 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-14-C语言-预处理
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-03 15:08:36" itemprop="dateCreated datePublished" datetime="2022-03-03T15:08:36+08:00">2022-03-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>28 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言基础——预处理相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-14-C%E8%AF%AD%E8%A8%80-%E9%A2%84%E5%A4%84%E7%90%86/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">#pragma、#error指令</td><td align="left"><a href="https://blog.51cto.com/u_15244533/2845287" target="_blank">C语言 | 认识认识#pragma、#error指令_wx60b650682e725的技术博客_51CTO博客 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">#pragma的使用方法</td><td align="left"><a href="https://www.jb51.net/article/245061.htm" target="_blank">C语言 详细讲解#pragma的使用方法_C 语言_脚本之家 (jb51.net) <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">#pragma的用法</td><td align="left"><a href="https://www.cnblogs.com/fnlingnzb-learner/p/5854494.html" target="_blank">#pragma的用法 - Boblim - 博客园 (cnblogs.com) <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">#paragma详解</td><td align="left"><a href="https://blog.csdn.net/weixin_30561425/article/details/96253036" target="_blank">#paragma详解_Surenon的博客-CSDN博客 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">内存对齐</td><td align="left"><a href="https://zhuanlan.zhihu.com/p/30007037" target="_blank">C/C++内存对齐详解 - 知乎 (zhihu.com) <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、预处理的概念"><a href="#一、预处理的概念" class="headerlink" title="一、预处理的概念"></a><font size=3>一、预处理的概念</font></h1><p>预处理就是指在进行编译的第一遍扫描（词法扫描和语法分析）之前所做的工作，它由预处理程序负责完成。当编译一个程序时，系统将自动调用预处理程序对程序中的 # 开头的预处理部分进行处理，处理完毕之后才会进入程序的编译阶段。</p>
<p>C 语言为我们提供了多种预处理功能，如宏定义，文件包含和条件编译等。</p>
<h1 id="二、预定义"><a href="#二、预定义" class="headerlink" title="二、预定义"></a><font size=3>二、预定义</font></h1><h2 id="1-宏"><a href="#1-宏" class="headerlink" title="1.宏"></a><font size=3>1.宏</font></h2><p>在 C 语言源程序中，允许使用一个标识符来表示一串符号，称之为<strong>宏</strong>，被定义为宏的标识符称为<strong>宏名</strong>。在编译预处理的时候，对程序中出现的所有宏名，都会用宏定义中的符号串去代替，这被称为<strong>宏替换</strong>或者叫<strong>宏展开</strong>。</p>
<h2 id="2-预定义符号串"><a href="#2-预定义符号串" class="headerlink" title="2.预定义符号串"></a><font size=3>2.预定义符号串</font></h2><p>在 C 语言中，有一些预定义的符号串，它们的值是字符串常量或者是十进制数字常量，通常是用于在调试程序时输出源程序的各项信息。</p>
<table>
    <tr><td align="center" width=100px>符号</td><td align="center" width=100px>常量类型</td><td align="center">含义</td></tr>
    <tr><td align="center" width=100px>__FILE__</td><td align="center" width=100px>字符串</td><td align="left">正在预编译编译的文件名（字符串字面值）</td></tr>
    <tr><td align="center" width=100px>__LINE__</td><td align="center" width=100px>整数</td><td align="left">文件的当前行号（十进制常量）</td></tr>
    <tr><td align="center" width=100px>__DATE__</td><td align="center" width=100px>字符串</td><td align="left">文件被编译的日期</td></tr>
    <tr><td align="center" width=100px>__TIME__</td><td align="center" width=100px>字符串</td><td align="left">文件被编译的时间</td></tr>
    <tr><td align="center" width=100px>__STDC__</td><td align="center" width=100px>整数</td><td align="left">如果编译器遵循ANSI C，其值为1，否则未定义</td></tr>
    <tr><td align="center" width=100px>__FUNCTION__</td><td align="center" width=100px>字符串</td><td align="left">表示调用此预定义的函数名称</td></tr>
    <tr><td align="center" width=100px>__func__</td><td align="center" width=100px>字符串</td><td align="left">表示调用此预定义的函数名称</td></tr>
</table>



<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __FILE__ is : %s\n&quot;</span>, __FILE__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __LINE__ is : %d\n&quot;</span>, __LINE__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __DATE__ is : %s\n&quot;</span>, __DATE__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __TIME__ is : %s\n&quot;</span>, __TIME__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __STDC__ is : %d\n&quot;</span>, __STDC__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __FUNCTION__ is : %s\n&quot;</span>, __FUNCTION__);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The __func__ is : %s\n&quot;</span>, __func__);</span><br><span class="line">	</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>然后我们会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">The __FILE__ is : test.c</span><br><span class="line">The __LINE__ is : 6</span><br><span class="line">The __DATE__ is : Mar 23 2022</span><br><span class="line">The __TIME__ is : 09:31:59</span><br><span class="line">The __STDC__ is : 1</span><br><span class="line">The __FUNCTION__ is : main</span><br><span class="line">The __func__ is : main</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="3-宏定义"><a href="#3-宏定义" class="headerlink" title="3.宏定义"></a><font size=3>3.宏定义</font></h2><p>除了预定义的符号外，我们也可以自己定义宏，宏定义就是<strong>用一个标识符来表示一个字符串</strong>，如果在后面的代码中出现了该标识符，那么就<strong>全部替换</strong>成指定的字符串。</p>
<p><strong>【注意】</strong></p>
<p>（1）这里的字符串字可以是常数、表达式、格式串等。不仅仅是代表“字符串”。</p>
<p>（2）如果说，“字符串”是一个含参的表达式，一定要注意，先替换，再运算。</p>
<h3 id="3-1无参宏定义"><a href="#3-1无参宏定义" class="headerlink" title="3.1无参宏定义"></a><font size=3>3.1无参宏定义</font></h3><p>无参宏定义的宏名，也就是标识符，后边不带参数，定义的一般形式是：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> 标识符 字符串</span></span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看各部分说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=100px>#</td>        <td align="left">表示这是一条预处理命令</td>    </tr>    <tr>        <td align="center" width=100px>define</td>        <td align="left">宏定义命令</td>    </tr>    <tr>        <td align="center" width=100px>标识符</td>        <td align="left">所定义的宏名（习惯上用全大写表示，但是也允许小写，看个人喜好喽），后边在程序中使用的宏都是直接使用宏名</td>    </tr>    <tr>        <td align="center" width=100px>字符串</td>        <td align="left">可以是常数，表达式，格式串等<br>【说明】 字符串 与 (字符串) 似乎没有区别。</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> pi 3.1415926</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> str <span class="string">&quot;Hello,world!&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> y (x + 3)</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> x = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    sum = <span class="number">5</span> * y;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The pi is : %f\n&quot;</span>, pi);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The str is : %s\n&quot;</span>, str);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The y is : %d\n&quot;</span>, y);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The sum is : %d\n&quot;</span>, sum);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">The pi is : 3.141593</span><br><span class="line">The str is : Hello,world!</span><br><span class="line">The y is : 3</span><br><span class="line">The sum is : 15</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【说明】</p>
<p>（1）宏定义用宏名来表示一串符号，在宏展开的时候又以该符号串取代宏名，这只是一种简单的替换，符号串中可以包含任何字符，可以是常数，也可以是表达式，<strong>预处理程序不对它做任何检查</strong>。如果有错误的话，只能在编译已被宏展开后的源程序时被发现。</p>
<p>（2）宏定义不是声明或者语句，行尾不必加分号(   ;   )，如果加上分号(   ;   )了，就会连分号(   ;   )一起替换。不过我在测试的时候是直接报错了，这里注意一下就好啦。</p>
<p>（3）宏定义的作用域包括从宏定义命名起到源程序结束，如果要终止其作用域，我们可以使用 #undef 来取消宏作用域。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> pi 3.1415926</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">fun1</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The pi is : %f\n&quot;</span>, pi);</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">undef</span> pi</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">fun2</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;The pi is : %f\n&quot;</span>, pi);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    fun1();</span><br><span class="line">    fun2();</span><br><span class="line">    </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>其中 pi 只会在 fun1() 中生效，会直接在 fun2() 定义时报错，会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">test.c: In function ‘fun2’:</span><br><span class="line">test.c:12:32: error: ‘pi’ undeclared (first use in this function)</span><br><span class="line">     printf(&quot;The pi is : %f\n&quot;, pi);</span><br><span class="line">                                ^~</span><br><span class="line">test.c:12:32: note: each undeclared identifier is reported only once for each function it appears in</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>（4）宏名引用时不要写在 “ “ 中，否则预处理程序不会对其进行替换。</p>
<p>（5）宏定义允许嵌套，在宏定义的符号串中就可以使用已经定义过的宏名，在宏展开时由预处理程序进行层层替换。</p>
<p>（6）可以对输出做一个宏定义，以减少编写麻烦。但是这样格式就不能自己想怎样就怎样了，不过还是要看自己需求啦。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> P printf</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> D <span class="string">&quot;%d\n&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> F <span class="string">&quot;%f\n&quot;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">5</span>;</span><br><span class="line">    <span class="type">float</span> b = <span class="number">3.1415926</span>;</span><br><span class="line"></span><br><span class="line">    P(D F, a, b);</span><br><span class="line">    </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">5</span><br><span class="line">3.141593</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2含参宏定义"><a href="#3-2含参宏定义" class="headerlink" title="3.2含参宏定义"></a><font size=3>3.2含参宏定义</font></h3><p>含参宏定义的宏名，也就是标识符，后边带参数，这个参数称为形参，调用的时候不仅要进行宏展开，还要传入实参。定义的一般形式是：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> 标识符(形参表) 字符串</span></span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看各部分说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=100px>#</td>        <td align="left">表示这是一条预处理命令</td>    </tr>    <tr>        <td align="center" width=100px>define</td>        <td align="left">宏定义命令</td>    </tr>    <tr>        <td align="center" width=100px>标识符</td>        <td align="left">所定义的宏名（习惯上用全大写表示，但是也允许小写，看个人喜好喽），后边在程序中使用的宏都是直接使用宏名</td>    </tr>    <tr>        <td align="center" width=100px>(形参表)</td>        <td align="left">形参的列表，可以有多个形参，用逗号 "," 分隔，形参最好用 () 括起来，以免出错</td>    </tr>    <tr>        <td align="center" width=100px>字符串</td>        <td align="left">可以是常数，表达式，格式串等<br>【说明】字符串 与  (字符串)  似乎没有区别，但是在含参的宏中最好用 () 括起来，减小出错的概率。</td>    </tr></table>
              </div>
            </details>

<p>调用的一般形式是：                                                                                                                                                                                                                                                                                                                                                                                           </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">标识符(实参表);</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong></p>
<p>（1）定义的时候不要带分号(   ;   )，调用的时候就是语句了，这就需要带上分号(   ;   )。</p>
<p>（2）注意<strong>先替换，再运算</strong>。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX(a, b) (a &gt; b)?a:b</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;MAX(a, b) is : %d\n&quot;</span>, MAX(a, b));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MAX(a, b) is : 5</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【说明】</p>
<p>（1）含参宏定义中，宏名和形参表之间不可以有空格。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> MAX (a, b) (a &gt; b)?a:b</span></span><br></pre></td></tr></table></figure><p>这在处理的时候直接报错了，其实按理来说，这句相当于是一个无参宏定义，宏名为 MAX ，它代表 (a, b) (a &gt; b)?a:b 。</p>
              </div>
            </details>

<p>（2）在含参的宏定义中，形参是不会被分配内存的，所以不必做类型的定义，这是与函数不同的。在含参宏定义中，这只是<strong>符号的替换，不存在值的传递</strong>。</p>
<p>（3）宏定义的形参相当于一个标识符，宏调用的实参可以是一个表达式。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX(a, b) ((a &gt; b)?a:b)</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;MAX(a, b) is : %d\n&quot;</span>, MAX(a + <span class="number">3</span>, b - <span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MAX(a, b) is : 6</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3宏与函数"><a href="#3-3宏与函数" class="headerlink" title="3.3宏与函数"></a><font size=3>3.3宏与函数</font></h3><p>含参的宏与函数有着很类似的形式，但是他们却有着很大的不同之处：</p>
<table>
    <tr><td align="center" width=120px>属性</td><td align="center">宏</td><td align="center">函数</td></tr>
    <tr><td align="center" width=120px>处理阶段</td><td align="left">预处理阶段，只是符号串的简单的替换</td><td align="left">编译阶段 </td></tr>
    <tr><td align="center" width=120px>代码长度</td><td align="left">每次使用宏时，宏代码都被插入到程序中。因此，除了非常小的宏之外，程序的长度都将被大幅增长 </td><td align="left">除了inline 函数之外，函数代码只出现在一个地方，每次使用这个函数，都只调用那个地方的同一份代码 </td></tr>
    <tr><td align="center" width=120px>执行速度</td><td align="left">更快</td><td align="left">存在函数调用/返回的额外开销（inline函数除外） </td></tr>
    <tr><td align="center" width=120px>操作符优先级</td><td align="left">宏参数的求值是在所有周围表达式的上下文环境中，除非它们加上括号，否则邻近操作符的优先级可能会产生不可预料的结果 </td><td align="left">函数参数只在函数调用时求值一次，它的结果值传递给函数，因此，表达式的求值结果更容易预测 </td></tr>
    <tr><td align="center" width=120px>参数求值</td><td align="left">参数每次用于宏定义时，它们都将重新求值。由于多次求值，具有副作用的参数可能会产生不可预料的结果</td><td align="left">参数在函数被调用前只求值一次，在函数中多次使用参数并不会导致多种求值问题，参数的副作用不会造成任何特殊的问题 </td></tr>
    <tr><td align="center" width=120px>参数类型</td><td align="left">宏与类型无关，只要对参数的操作是合法的，它可以使用任何参数类型</td><td align="left">函数的参数与类型有关，如果参数的类型不同，就需要使用不同的函数，即使它们执行的任务是相同的</td></tr>
</table>

<h3 id="3-4-与"><a href="#3-4-与" class="headerlink" title="3.4 # 与 ## "></a><font size=3>3.4 # 与 ## </font></h3><h4 id="3-4-1-的用途"><a href="#3-4-1-的用途" class="headerlink" title="3.4.1 # 的用途"></a><font size=3>3.4.1 # 的用途</font></h4><p># 的功能是将其后面的宏参数进行字符串化操作（ Stringfication ），简单说就是在对它所引用的宏变量，在预编译完成替换后同时在其左右各加上一个双引号。</p>
<p>例如，下边的测试程序：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> STR(s) #s</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> arc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> a[<span class="number">10</span>] = STR(mine);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们在终端中输入以下命令进行预编译：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc -E test.c -o test.i # 预编译</span><br><span class="line">vim test.i</span><br></pre></td></tr></table></figure>

<p>然后在文件的结束，我们会看到如下几行：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 前边的省略 ... ...</span><br><span class="line"># <span class="number">5</span> <span class="string">&quot;test.c&quot;</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> arc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="type">char</span> a[<span class="number">10</span>] = <span class="string">&quot;mine&quot;</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们会发现， mine 被替换为 “mine” 了。</p>
<h4 id="3-4-2-的用途"><a href="#3-4-2-的用途" class="headerlink" title="3.4.2 ## 的用途"></a><font size=3>3.4.2 ## 的用途</font></h4><p>后边学习过程中，遇到了一个定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __SOCKADDR_COMMON(sa_prefix) \</span></span><br><span class="line"><span class="meta">  sa_family_t sa_prefix##family</span></span><br></pre></td></tr></table></figure>

<p>当时看的我一脸懵逼，查阅资料后，了解到，在宏定义中， ## 也就是两个 # 连用，称为连接符，主要是用于连接两个参数， ## 符会把传递过来的参数当成字符串进行替代。例如，下边的测试程序：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> mine(prefix) int prefix##family</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONS(a,b) int(a##e##b)</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> arc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	mine(x_);</span><br><span class="line">	CONS(A, B);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们在终端中输入以下命令进行预编译：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc -E test.c -o test.i # 预编译</span><br><span class="line">vim test.i</span><br></pre></td></tr></table></figure>

<p>然后在文件的结束，我们会看到如下几行：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 前边的省略 ... ...</span><br><span class="line"># <span class="number">6</span> <span class="string">&quot;test.c&quot;</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> arc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line"> <span class="type">int</span> x_family;</span><br><span class="line"> <span class="type">int</span>(AeB);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到，我们传入的参数 x_ 在预编译后，与 fanily 连接起来了，变成了 x_family ，然后 a##e##b 变成了 AeB 。</p>
<h1 id="三、文件包含"><a href="#三、文件包含" class="headerlink" title="三、文件包含"></a><font size=3>三、文件包含</font></h1><p>文件包含是 C 语言预处理的另一种方式，文件包含的一般形式是：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;filename&gt;</span></span></span><br><span class="line"><span class="comment">/* 或者 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;filename&quot;</span></span></span><br></pre></td></tr></table></figure>

<p>有木有觉得很熟悉呢，这经常用于引入对应的头文件（ .h 文件）。</p>
<p>文件包含的处理过程很简单，就是<strong>将头文件的内容插入到该语句所在行的位置</strong>，从而把头文件和当前源文件连接成一个源文件，这与复制粘贴的效果相同。</p>
<details class="folding-tag" blue><summary> 点击查看两种写法的区别 </summary>
              <div class='content'>
              <p>使用尖括号 &lt; &gt; 和双引号 “ “ 的区别在于<strong>头文件的搜索路径</strong>不同：</p><ul><li>使用尖括号 &lt; &gt; ，编译器会到<strong>系统路径</strong>下查找头文件；</li><li>使用双引号 “ “ ，编译器首先在<strong>当前目录</strong>下查找头文件，如果没有找到，再到<strong>系统路径</strong>下查找。</li></ul><p>像 stdio.h  和  stdlib.h 这些都是标准头文件，它们存放于系统路径下，所以使用尖括号和双引号都能够成功引入；而我们自己编写的头文件，一般存放于当前项目的路径下，所以不能使用尖括号，只能使用双引号。</p>
              </div>
            </details>

<p><strong>【注意】</strong></p>
<p>（1）一个 include 命令只能指定一个被包含的文件，若有多个文件要包含，则需要用多个 include 命令。而且文件的包含允许嵌套，在一个被包含的文件中还可以包含别的文件。</p>
<p>（2）在使用我们自己编写的头文件时，也可以在 include 中直接指明路径，例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./include/test.h&quot;</span></span></span><br></pre></td></tr></table></figure>

<p>（3）同一个头文件可以被多次引入，多次引入的效果和一次引入的效果相同，因为头文件在代码层面有<strong>防止重复引入</strong>的机制。</p>
<h1 id="四、条件编译"><a href="#四、条件编译" class="headerlink" title="四、条件编译"></a><font size=3>四、条件编译</font></h1><p>当我们写的源代码是跨平台的，而代码每次只会在一个平台上运行，我们就全部编译所有代码吗？那样多少有些浪费。</p>
<p>其实 C 语言为我们提供了条件编译功能，以便于我们只编译需要的部分。所谓<strong>条件编译，就是能够根据不同情况编译不同代码、产生不同目标文件的机制</strong>。条件编译的关键字为 #if 、 #ifdef 和 #ifndef 。</p>
<h2 id="1-if"><a href="#1-if" class="headerlink" title="1. #if "></a><font size=3>1. #if </font></h2><h3 id="1-1使用格式"><a href="#1-1使用格式" class="headerlink" title="1.1使用格式"></a><font size=3>1.1使用格式</font></h3><p>使用的时候，可以有三种格式：</p>
<div class="tabs" id="tabname1"><ul class="nav-tabs"><li class="tab active"><a href="#tabname1-1">#if</a></li><li class="tab"><a href="#tabname1-2">#if...#else...</a></li><li class="tab"><a href="#tabname1-3">#if...#elif...</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname1-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 1.只有 #if */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 常量表达式</span></span><br><span class="line">	语句块;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果常量表达式为真( 1 )，则编译语句块，若常量表达式为假( 0 )，则不做处理。</p></div><div class="tab-pane" id="tabname1-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 2. #if ... #else... */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 常量表达式</span></span><br><span class="line">	语句块<span class="number">1</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">	语句块<span class="number">2</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果常量表达式为真( 1 )，则编译语句块 1 ，若常量表达式为假( 0 )，则编译语句块 2 。</p></div><div class="tab-pane" id="tabname1-3"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 3. #if ... #elif... */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 常量表达式1</span></span><br><span class="line">	语句块<span class="number">1</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">elif</span> 常量表达式2</span></span><br><span class="line">	语句块<span class="number">2</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span> 常量表达式3</span></span><br><span class="line">	语句块<span class="number">3</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果 常量表达式1 的值为真（非 0 ，也就是 1 ），就对 语句块1 进行编译；否则计算 常量表达式表达式2 ，结果为真就对 语句块2 进行编译；若为假就继续往下匹配，直到遇到值为真的表达式，或者遇到  #else 。</p></div></div></div>

<h3 id="1-2使用实例"><a href="#1-2使用实例" class="headerlink" title="1.2使用实例"></a><font size=3>1.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 1</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="meta">#<span class="keyword">if</span> N == 0</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;#if N==0\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">elif</span> N == 1</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;#elif N==1\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;#else\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="keyword">elif</span> N==1</span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【注意】 #if  命令要求判断条件为整型常量表达式，也就是说，表达式中不能包含变量，而且结果必须是整数。这是与 if 不同的地方。例如，若表达式结果为 1.3 ，将会报以下错误：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">error: floating constant in preprocessor expression</span><br></pre></td></tr></table></figure>

<h2 id="2-ifdef"><a href="#2-ifdef" class="headerlink" title="2. #ifdef "></a><font size=3>2. #ifdef </font></h2><h3 id="2-1使用格式"><a href="#2-1使用格式" class="headerlink" title="2.1使用格式"></a><font size=3>2.1使用格式</font></h3><p>使用的时候，常见的有两种格式，如下所示：</p>
<div class="tabs" id="tabname2"><ul class="nav-tabs"><li class="tab active"><a href="#tabname2-1">#ifdef</a></li><li class="tab"><a href="#tabname2-2">#ifdef...#else...</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname2-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 1.只有 #ifdef */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> macro</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> macro</span></span><br><span class="line">	语句块;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果宏 macro 定义了，则编译语句块，若宏 macro 未定义，则不做处理。</p></div><div class="tab-pane" id="tabname2-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 2. #ifdef ... #else... */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> macro</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> macro</span></span><br><span class="line">	语句块<span class="number">1</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">	语句块<span class="number">2</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果宏 macro 定义了，则编译语句块 1 ，若宏 macro 未定义，则编译语句块 2 。</p></div></div></div>

<h3 id="2-2使用实例"><a href="#2-2使用实例" class="headerlink" title="2.2使用实例"></a><font size=3>2.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEBUG1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEBUG2 0</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="meta">#<span class="keyword">ifdef</span> DEBUG1</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG1 is define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG1 is not define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    </span><br><span class="line">    <span class="meta">#<span class="keyword">ifdef</span> DEBUG2</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG2 is define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG2 is not define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">DEBUG1 is define</span><br><span class="line">DEBUG2 is define</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【注意】这种情况定义宏的话，可以有字符串替换宏名，也可以没有。</p>
<h2 id="3-ifndef"><a href="#3-ifndef" class="headerlink" title="3. #ifndef "></a><font size=3>3. #ifndef </font></h2><h3 id="3-1使用格式"><a href="#3-1使用格式" class="headerlink" title="3.1使用格式"></a><font size=3>3.1使用格式</font></h3><p>使用的时候，一般格式如下：</p>
<div class="tabs" id="tabname3"><ul class="nav-tabs"><li class="tab active"><a href="#tabname3-1">#ifndef</a></li><li class="tab"><a href="#tabname3-2">#ifndef...#else...</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname3-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 1.只有 #ifndef */</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> macro</span></span><br><span class="line">	语句块;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果宏 macro 没有被定义，则编译语句块，若宏 macro 被定义，则不做处理。</p></div><div class="tab-pane" id="tabname3-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 2. #ifndef ... #else... */</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> macro</span></span><br><span class="line">	语句块<span class="number">1</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">	语句块<span class="number">2</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>【说明】如果宏 macro 没有被定义，则编译语句块 1 ，若宏 macro 被定义，则编译语句块 2 。</p></div></div></div>

<h3 id="3-2使用实例"><a href="#3-2使用实例" class="headerlink" title="3.2使用实例"></a><font size=3>3.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEBUG1</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="meta">#<span class="keyword">ifndef</span> DEBUG1</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG1 is not define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG1 is define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    </span><br><span class="line">    <span class="meta">#<span class="keyword">ifndef</span> DEBUG2</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG2 is not define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;DEBUG2 is define\n&quot;</span>);</span><br><span class="line">    <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">EBUG1 is define</span><br><span class="line">DEBUG2 is not define</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【注意】这种情况定义宏的话，可以有字符串替换宏名，也可以没有。</p>
<h1 id="五、-pragma"><a href="#五、-pragma" class="headerlink" title="五、 #pragma "></a><font size=3>五、 #pragma </font></h1><h2 id="4-1简介"><a href="#4-1简介" class="headerlink" title="4.1简介"></a><font size=3>4.1简介</font></h2><p> #pragma  用于指示编译器完成一些特定的动作，它所定义的很多指示字是编译器特有的，并且在不同的编译器间是不可移植的。预处理器将会忽略它不认识的 #pragma 指令，不同的编译器可能会使用不同的方式解释同一条 #pragma 指令。</p>
<p> #pragma 指令应该是预处理指令中最复杂的，其用法很多。</p>
<h2 id="4-2-message"><a href="#4-2-message" class="headerlink" title="4.2 message "></a><font size=3>4.2 message </font></h2><h3 id="4-2-1语法格式"><a href="#4-2-1语法格式" class="headerlink" title="4.2.1语法格式"></a><font size=3>4.2.1语法格式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> message(<span class="string">&quot;string&quot;</span>)</span></span><br></pre></td></tr></table></figure>

<p>该参数可以在编译信息输出窗口中输出相应的信息。</p>
<h3 id="4-2-2使用实例"><a href="#4-2-2使用实例" class="headerlink" title="4.2.2使用实例"></a><font size=3>4.2.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> message(<span class="string">&quot;This is pragma message!&quot;</span>)</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;hello World!\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">test.c:3:9: note: ‘#pragma message: This is pragma message!’</span><br><span class="line">    3 | #pragma message(&quot;This is pragma message!&quot;)</span><br><span class="line">      |         ^~~~~~~</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-3-once"><a href="#4-3-once" class="headerlink" title="4.3 once "></a><font size=3>4.3 once </font></h2><h3 id="4-3-1语法格式"><a href="#4-3-1语法格式" class="headerlink" title="4.3.1语法格式"></a><font size=3>4.3.1语法格式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> once</span></span><br></pre></td></tr></table></figure>

<p>该参数用于保证头文件只被编译一次，它与编译器相关，不一定被编译器所支持。还记得之前我们定义头文件的时候使用的是以下形式</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> __HEAD_FILENAME_H__</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __HEAD_FILENAME_H__</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p>它与使用 #pragma once 的区别在于前者是 C  语言所支持的，并不是只包含一次头文件，而是会包含多次，然后通过宏控制是否嵌入到源代码中，也就是说通过宏的方式，可以保证头文件里面的内容只被嵌入一次，但是由于包含了多次，预处理器还是处理了多次，所以效率上来说比较低；后者是告诉预处理器当前文件只编译一次，所以说效率较高。</p>
<p>如果说既想要保证移植性，又想要保证效率，我们可以两种方式同时使用：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> __HEAD_FILENAME_H__</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __HEAD_FILENAME_H__</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> once</span></span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>



<h3 id="4-3-2使用实例"><a href="#4-3-2使用实例" class="headerlink" title="4.3.2使用实例"></a><font size=3>4.3.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <div class="tabs" id="tabname4"><ul class="nav-tabs"><li class="tab active"><a href="#tabname4-1">test.c</a></li><li class="tab"><a href="#tabname4-2">global.h</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname4-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;global.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;global.h&quot;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a=%d\n&quot;</span>, a);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname4-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> once</span></span><br><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br></pre></td></tr></table></figure></div></div></div><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall</span><br></pre></td></tr></table></figure><p>然后若是我们没有在 global.h 中添加 #pragma once 的话，会有以下信息产生：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In file included from test.c:3:</span><br><span class="line">global.h:1:5: error: redefinition of ‘a’</span><br><span class="line">    1 | int a = 10;</span><br><span class="line">      |     ^</span><br><span class="line">In file included from test.c:2:</span><br><span class="line">global.h:1:5: note: previous definition of ‘a’ was here</span><br><span class="line">    1 | int a = 10;</span><br><span class="line">      |     ^</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>我们发现报错了， a 出现了重复定义，我们在 global.h 中加上 #pragma once 之后，便不会再有报错。我们在终端执行 .&#x2F;a,out 会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">a=10</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-4-pack"><a href="#4-4-pack" class="headerlink" title="4.4 pack "></a><font size=3>4.4 pack </font></h2><p>后边在自定义数据类型的地方还会用到这个参数。</p>
<h3 id="4-4-1内存对齐"><a href="#4-4-1内存对齐" class="headerlink" title="4.4.1内存对齐"></a><font size=3>4.4.1内存对齐</font></h3><blockquote>
<p>什么是内存对齐？</p>
</blockquote>
<p>我使用的 64 位 Ubuntu 中， int 类型占 4 字节， char 类型占 1 字节，当他们出现在一个结构体（后边会学习到）中应该是 5 字节，但是实际上却会是 8 字节，这就是内存对齐导致的。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> a;</span><br><span class="line">	<span class="type">int</span> b;</span><br><span class="line">&#125; S1;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(S1)=%ld, sizeof(S1.a)=%ld, sizeof(S1.b)=%ld\n&quot;</span>, <span class="keyword">sizeof</span>(S1), <span class="keyword">sizeof</span>(S1.a), <span class="keyword">sizeof</span>(S1.b));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall</span><br><span class="line">./a.out</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sizeof(S1)=8, sizeof(S1.a)=1, sizeof(S1.b)=4</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<blockquote>
<p>为什么要内存对齐？</p>
</blockquote>
<p>内存是以字节为单位，但是大部分处理器并不是按字节块来存取内存的。它一般会以 2 、 4 、 6 、 8 甚至 32 字节为单位来存取内存.现在以每次存取 4 字节的处理器为例分析，取 int 类型变量（ 64 位系统），该处理器只能从地址为 4 的倍数的内存开始读取数据。</p>
<p>假如没有内存对齐机制，数据可以任意存放，现在一个 int 变量存放在从地址 1 开始的连续 4 个字节字节地址中，该处理器去取数据时，要先从 0 地址开始读取第一个 4 字节块，并剔除不想要的字节（ 0 地址）,然后从地址 4 开始读取下一个 4 字节块，同样需要删除不要的数据（也就是 5 ， 6 ， 7 地址处的数据）,最后留下的 2 块数据便是我们存放的 int 类型数据，这就意味着处理器要进行大量的处理，存取数据效率将会很低。</p>
<p>现在有了内存对齐， int 类型数据只能存放在按照对齐规则的内存中，比如说 0 地址开始的内存。那么现在该处理器在取数据时一次性就能将数据读出来了，而且不需要做额外的操作，提高了效率。内存对齐在结构体和联合体的大小计算中会得到很好的体现。</p>
<blockquote>
<p>对齐规则？</p>
</blockquote>
<p>编译器都有自己的默认“对齐系数”（也叫对齐模数）。 gcc 中默认 #pragma pack(4) ，可以通过预编译命令 #pragma pack(n) ， n &#x3D; 1,2,4,8,16 来改变这一系数。</p>
<p>给定值 #pragma pack(n) 和结构体中最长数据类型长度中较小的那个被称之为有效对齐值，也叫<strong>对齐单位</strong>。</p>
<h3 id="4-4-2语法格式"><a href="#4-4-2语法格式" class="headerlink" title="4.4.2语法格式"></a><font size=3>4.4.2语法格式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack(n)      <span class="comment">/* 设置编辑器按照n个字节对齐，n可以取值1,2,4,8,16 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack()       <span class="comment">/* 取消自定义字节对齐方式。 */</span></span></span><br></pre></td></tr></table></figure>

<p>该参数可以改变编译器默认的字节对齐方式。</p>
<h3 id="4-4-2使用实例"><a href="#4-4-2使用实例" class="headerlink" title="4.4.2使用实例"></a><font size=3>4.4.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack(1) <span class="comment">/* 字节对齐改成了1个字节 */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack() <span class="comment">/* 取消自定义字节对齐 */</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Test</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct Student) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.name) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.gender) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.age) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.id) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.score) = %.ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.score));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Test</span> <span class="title">stu2</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct Student) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.name) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.gender) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.age) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.id) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.score) = %.ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.score));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall</span><br><span class="line">./a.out</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">sizeof(stu1) = 20</span><br><span class="line">sizeof( struct Student) = 20</span><br><span class="line">sizeof(stu1.name) = 8</span><br><span class="line">sizeof(stu1.gender) = 1</span><br><span class="line">sizeof(stu1.age) = 4</span><br><span class="line">sizeof(stu1.id) = 3</span><br><span class="line">sizeof(stu1.score) = 4</span><br><span class="line"></span><br><span class="line">sizeof(stu2) = 24</span><br><span class="line">sizeof( struct Student) = 20</span><br><span class="line">sizeof(stu2.name) = 8</span><br><span class="line">sizeof(stu2.gender) = 1</span><br><span class="line">sizeof(stu2.age) = 4</span><br><span class="line">sizeof(stu2.id) = 3</span><br><span class="line">sizeof(stu2.score) = 4</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="六、-error"><a href="#六、-error" class="headerlink" title="六、 #error "></a><font size=3>六、 #error </font></h1><h2 id="1-使用格式"><a href="#1-使用格式" class="headerlink" title="1.使用格式"></a><font size=3>1.使用格式</font></h2><p> #error 也是一个预处理命令，当编译器遇到 #error 的时候将停止编译，并输出自定义的消息，一般使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">error</span> [自定义的错误消息]</span></span><br></pre></td></tr></table></figure>

<p>其中  [] 中的内容是可选的，也可以不输出提示信息。我们可以使用该预处理指令来停止编译，保证程序是按照我们所设想的那样进行编译的，以免产生不可预料的后果。</p>
<p><strong>【注意】</strong></p>
<p>（1）自定义的错误消息不需要加引号 “ “ ，如果加上的话，引号会被一起输出。</p>
<p>（2）当程序比较大时，往往有些宏定义是在外部指定的（如 makefile ），或是在系统头文件中指定的。</p>
<h2 id="2-使用实例"><a href="#2-使用实例" class="headerlink" title="2.使用实例"></a><font size=3>2.使用实例</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MACRO 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> MACRO == 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">error</span> MACRO=1</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;hello world!\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当我们编译的时候，会有如下提示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">gcc main.c -Wall -o main</span><br><span class="line">main.c:5:2: error: #error MACRO=1</span><br><span class="line"><span class="meta prompt_"> #</span><span class="language-bash">error MACRO=1</span></span><br><span class="line">  ^</span><br><span class="line">Makefile:2: recipe for target &#x27;all&#x27; failed</span><br><span class="line">make: *** [all] Error 1</span><br></pre></td></tr></table></figure>

<h1 id="七、易错点"><a href="#七、易错点" class="headerlink" title="七、易错点"></a><font size=3>七、易错点</font></h1><h2 id="1-案例1"><a href="#1-案例1" class="headerlink" title="1.案例1"></a><font size=3>1.案例1</font></h2><blockquote>
<p><strong>【题目】</strong></p>
<p>有以下宏定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> M(x, y, z) x*y+z</span></span><br></pre></td></tr></table></figure>

<p>在主程序有以下语句：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sum = M(a + b, b + c, c + a);</span><br></pre></td></tr></table></figure>

<p>若 a &#x3D; 1, b &#x3D; 2, c &#x3D; 3，则sum为多少？</p>
</blockquote>
<blockquote>
<p><strong>【解答】</strong></p>
<p>错误解答：sum &#x3D; (a + b) * (b + c) + (c + a) &#x3D; 3 * 5 + 4 &#x3D; 19</p>
<p>正确解答：sum &#x3D; a + b * b + c + c + a) &#x3D;  1 + 2 * 2 + 3 + 3 + 1 &#x3D; 12</p>
</blockquote>
<p><strong>【注意】</strong>一定是先替换，后运算，不可直接运算。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/73483bf2.html">LV01-14-C语言-预处理</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年03月03日 - 15:08</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/73483bf2.html" title="LV01-14-C语言-预处理">https://sumumm.github.io/post/73483bf2.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/f72c7684.html" rel="prev" title="LV01-15-C语言-自定义数据类型">
                  <i class="fa fa-angle-left"></i> LV01-15-C语言-自定义数据类型
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/a638936c.html" rel="next" title="LV02-04-GDB-调试工具">
                  LV02-04-GDB-调试工具 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
